package com.miaomiaojia.android;

import com.miaomiaojia.bll.CommonInterface;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class LoginActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		SetLoginButton(R.id.btnsina, "sina");
		SetLoginButton(R.id.btnbaidu, "baidu");
		SetLoginButton(R.id.btnqq, "ten");
		SetLoginButton(R.id.btnkaixin, "kaixin");
		SetLoginButton(R.id.btnqqweibo, "tenweibo");
		SetLoginButton(R.id.btntaobao, "taobao");
		SetLoginButton(R.id.btnrenren, "renren");
	}

	private void SetLoginButton(int id, final String type) {
		findViewById(id).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String loginUrl = getResources().getString(R.string.Login);
				loginUrl = String.format(loginUrl, type);
				Bundle urlBundle = new Bundle();
				urlBundle.putString("url", loginUrl);
				Intent loginIntent = new Intent(LoginActivity.this,
						BrowserActivity.class);
				loginIntent.putExtras(urlBundle);

				startActivityForResult(loginIntent,
						CommonInterface.RequestCode.REQUEST_LOGIN);
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CommonInterface.RequestCode.REQUEST_LOGIN:
			if (resultCode == CommonInterface.LoginResult.LOGIN_OK) {
				setResult(CommonInterface.LoginResult.LOGIN_OK, null);
				LoginActivity.this.finish();
			} else {

			}
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_browser, menu);
		return true;
	}
}
